package com.datastructure.test.maxvalueofgift;

public class MaxValueOfGift {

    public static void main(String[] args) {

    }

    public int maxValue (int[][] grid) {
        // write code here
        int[][] data = new int[grid.length][grid[0].length];
        data[0][0]=grid[0][0];
        int top = 0;
        int left = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(i==0&&j>0){
                    data[i][j]=data[i][j-1]+grid[i][j];
                }
                if(j==0&&i>0){
                    data[i][j]=data[i-1][j]+grid[i][j];
                }
                if(i>=1&&j>=1){
                    top=data[i-1][j];
                    left=data[i][j-1];
                    data[i][j]=Math.max(left,top)+grid[i][j];
                }
            }
        }
        return data[grid.length-1][grid[0].length-1];
    }
}
